<?php
# Example MediaWiki extension
# with MediaWiki's extension mechanism it is possible to define
# new tags of the form
# <TAGNAME> some text </TAGNAME>
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
#       included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
 
$wgExtensionFunctions[] = "topnewsxml";

# my new additions

# an empty function in case we need to clean up database results
function preprocessResults($dbr,$res)
{}


/**
 * Format and output report results using the given information plus
 * OutputPage
 *
 * @param OutputPage $out OutputPage to print to
 * @param Database $dbr Database (read) connection to use
 * @param int $res Result pointer
 * @param int $num Number of available result rows
 * @param int $offset Paging offset
 */

function outputResults( $output, $dbr, $res, $num, $offset ) {

	if( $num > 0 ) 
	{
		$html = "<ol>";
			
		# $res might contain the whole 1,000 rows, so we read up to
		# $num [should update this to use a Pager]
		for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++ )
		 {
			$line = $row->title.' '.$row->value;
			$html.="<li>{$line}</li>\n";
		}

		return $html.'</ol>';
	}
}
	

function doQuery( $off=0, $lim=10, $shownavigation=false, $output )
{
	global $wgUser, $wgOut, $wgLang, $wgContLang;
	$offset = $off;
	$limit = $lim;

	$dbr = wfGetDB( DB_SLAVE );
	$wgOut->setSyndicated('false');

	# generates SQL statement, this is where we can write a query
	#list ($revision, $page) = $dbr->tableNamesN('revision','page');

	$sql = /*$this->getSQL();*/ "SELECT 'topnews' as type,
						page_namespace as namespace,
						page_title as title,
						page_counter as value
					FROM revision
					JOIN page ON page_id = rev_page
					WHERE page_namespace = " . NS_MAIN . "
					GROUP BY 1,2,3
					HAVING COUNT(*) > 1";

	
	# adds options onto end of SQL query
	# $sql .= /*$this->getOrder();*/ 'ORDER BY value DESC ';
	# $sql = $dbr->limitResult($sql, $limit, $offset);
	
	
	$res = $dbr->query( $sql );
	$num = $dbr->numRows($res);

	# doesn't do anything right now (no pre-processing)
	preprocessResults( $dbr, $res );

	# commented out because don't see what it does
	# $sk = $wgUser->getSkin();
		
		
	# The actual results; specialist subclasses will want to handle this
	# with more than a straight list, so we hand them the info, plus
	# an OutputPage, and let them get on with it
	$html = outputResults( $output,
		$dbr,
		$res,
		$dbr->numRows( $res ),
		$offset );

	return $html;
}
	


 
function topnewsxml()
 {
    global $wgParser;
    # register the extension with the WikiText parser
    # the first parameter is the name of the new tag.
    # In this case it defines the tag <example> ... </example>
    # the second parameter is the callback function for
    # processing the text between the tags
    $wgParser->setHook( "topnews", "render" );
}
 
# The callback function for converting the input text to HTML output
function render( $input, $argv, &$parser ) {
    # $argv is an array containing any arguments passed to the
    # extension like <example argument="foo" bar>..
    # Put this on the sandbox page:  (works in MediaWiki 1.5.5)
    #   <example argument="foo" argument2="bar">Testing text **example** in between the new tags</example>
    $output = "Text passed into example extension: <br/>$input";
    # $output .= " <br/> and the value for the arg 'argument' is " . $argv["argument"];
    
	
	# main call
	$output.= doQuery(0,10,false,$output);

	# returns html output
    	return $output;
}



?>
